package com.wzy.recursive;

import com.alibaba.fastjson.JSONObject;

import java.util.*;

/**
 * @author wzy
 * @Date: 2022/3/18 15:15
 * @Description:
 */
public class Test {

    public static void main(String[] args) {
        List<People> list = new ArrayList<>();
        People people = new People();
        people.setName("教导主任");
        people.setLevel("高级");
        List<People> peopleList = new ArrayList<>();

        People people1 = new People();
        people1.setName("张老师");
        people1.setLevel("初级老师");
        List<People> peopleList1 = new ArrayList<>();
        People s1 = new People();
        s1.setName("AAA");
        s1.setLevel("A");
        People s2 = new People();
        s2.setName("BBB");
        s2.setLevel("B");
        People s3 = new People();
        s3.setName("CCC");
        s3.setLevel("C");
        peopleList1.add(s1);
        peopleList1.add(s2);
        peopleList1.add(s3);
        people1.setPeopleList(peopleList1);
        People people2 = new People();
        people2.setName("李老师");
        people2.setLevel("高级老师");
        List<People> peopleList2 = new ArrayList<>();
        People s4 = new People();
        s4.setName("DDD");
        s4.setLevel("D");
        People s5 = new People();
        s5.setName("EEE");
        s5.setLevel("E");
        peopleList2.add(s4);
        peopleList2.add(s5);
        people2.setPeopleList(peopleList2);
        peopleList.add(people1);
        peopleList.add(people2);
        people.setPeopleList(peopleList);

        list.add(people);

        System.out.println("************************************");
        System.out.println(JSONObject.toJSONString(people));
        System.out.println("************************************");


        People p2 = new People();
        p2.setName("董事长");
        p2.setLevel("boss");
        List<People> p2s = new ArrayList<>();

        People t1 = new People();
        t1.setName("张经理");
        t1.setLevel("商务经理");
        List<People> t1List = new ArrayList<>();
        People r1 = new People();
        r1.setName("画虎");
        r1.setLevel("专员");
        t1List.add(r1);
        t1.setPeopleList(t1List);

        People t2 = new People();
        t2.setName("李总监");
        t2.setLevel("技术总监");
        List<People> t2List = new ArrayList<>();
        People p1 = new People();
        p1.setName("王小二");
        p1.setLevel("高级开发");
        t2List.add(p1);
        p2s.add(t1);
        p2s.add(t2);
        p2.setPeopleList(p2s);
        System.out.println("************************************");
        System.out.println(JSONObject.toJSONString(p2));
        System.out.println("************************************");
        list.add(p2);

        System.out.println("************************************");
        System.out.println(JSONObject.toJSONString(list));
        System.out.println("************************************");


        List<People> init = init(JSONObject.toJSONString(list));

        System.out.println(JSONObject.toJSONString(init));

    }

    private static List<People> init(String peopleList) {

        // 递归
        /*List<People> peoples = new ArrayList<>();
        List<Map> objects = JSONObject.parseArray(peopleList, Map.class);
        for (Map object : objects) {
            People people = new People();
            people.setLevel((String) object.get("level"));
            people.setName((String) object.get("name"));
            Object str = object.get("peopleList");
            if (Objects.nonNull(str)) {
                people.setPeopleList(init(JSONObject.toJSONString(str)));
            }
            peoples.add(people);
        }
        return peoples;*/

        // 迭代  待实现

        List<People> peoples = new ArrayList<>();
        List<Map> objects = JSONObject.parseArray(peopleList, Map.class);
        for (Map object : objects) {
            People people = new People();
            people.setLevel((String) object.get("level"));
            people.setName((String) object.get("name"));
            LinkedList<People> stack = new LinkedList<>();
            stack.push(people);
            while (!stack.isEmpty()){
                Object str = object.get("peopleList");
                List<Map> temp = JSONObject.parseArray(JSONObject.toJSONString(str), Map.class);
                List<People> peopleList1 = new ArrayList<>();
                for (Map map : temp) {
                    People people1 = new People();
                    people1.setLevel((String) map.get("level"));
                    people1.setName((String) map.get("name"));
                    if (Objects.nonNull(map.get("peopleList"))){
                        str = map.get("peopleList");
                        stack.push(people1);
                    }
                    peopleList1.add(people1);
                }
            }
            peoples.add(stack.pop());
        }
        return null;

    }
}
